﻿package com.thorqq.magictimer.action;

import android.content.Context;
import android.media.AudioManager;

import com.thorqq.magictimer.core.TAction;
import com.thorqq.magictimer.core.TimerAbs;
import com.thorqq.magictimer.util.Util;

/**
 * 调整铃声模式：常规、静音、震动
 * 
 */
public class ActionRingerMode extends TAction
{
    private static final long serialVersionUID = -726547719215899225L;

    protected final int PARAM_NUM = 1;
    
    private int mMode = AudioManager.RINGER_MODE_NORMAL;

    public ActionRingerMode(int id, int execOrder, String actionType, String param)
    {
        super(id, execOrder, actionType, param);
    }

    @Override
    public boolean run(Context context, TimerAbs timer)
    {
        AudioManager audioMgr = (AudioManager)context.getSystemService(Context.AUDIO_SERVICE);
        int mode = audioMgr.getRingerMode();
        
        if(mMode != mode)
        {
            audioMgr.setRingerMode(mMode);
            Util.log("Switch ringer mode to " + mMode);
        }
        else
        {
            Util.log("Don't need to switch ringer mode.");
        }

        return true;
    }

    @Override
    public String getName()
    {
        switch(mMode)
        {
        case AudioManager.RINGER_MODE_NORMAL: return "转为铃声";
        case AudioManager.RINGER_MODE_SILENT: return "转为静音";
        case AudioManager.RINGER_MODE_VIBRATE: return "转为震动";
        default: return null;
        }
    }

    @Override
    public String getDescription()
    {
        return null;
    }

    public void setParam(int mode)
    {
        mMode = mode;

        this.paramToString();
    }

    @Override
    public boolean parseStringParam()
    {
        String[] array = Util.splitParames(mParam);
        if (array.length != PARAM_NUM)
        {
            Util.log("TimeParam setParam error. param = " + mParam);
            return false;
        }

        mMode = Util.strToInt(array[0]);

        return true;
    }

    @Override
    public String paramToString()
    {
        StringBuffer sff = new StringBuffer();
        sff.append(mMode);

        return super.mParam = sff.toString();
    }

}
